package com.sls.game.parser.snns;

import java.util.Iterator;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sls.game.pojo.snns.SevenNineNineSixRespInfo;

public class ParseResponseInfo {

	private static Logger log = Logger.getLogger(ParseResponseInfo.class);

	@SuppressWarnings("unchecked")
	public static SevenNineNineSixRespInfo parse(String xml) {
		SevenNineNineSixRespInfo resInfo = null;
		Document document = null;
		Element root = null;
		// 读取XML文件,获得document对象
		try {
			document = DocumentHelper.parseText(xml);
			root = document.getRootElement();
			Iterator<Element> it = root.elementIterator();
			while (it.hasNext()) {
				resInfo = new SevenNineNineSixRespInfo();
				Element ele = it.next();
				String tagName = ele.getName();
				String result = "";
				if ("result".equals(tagName)) {
					result = ele.getTextTrim();
					resInfo.setResult(result);
				} else if ("msg".equals(tagName)) {
					String msg = ele.getTextTrim();
					resInfo.setMsg(msg);
				}
				if ("data".equals(tagName)) {
					Element e = root.element("result");
					String rs = e.getTextTrim();
					if ("true".equals(rs)) {
						Iterator<Element> it2 = ele.elementIterator();
						while (it2.hasNext()) {
							Element elem = it2.next();
							String tagName2 = elem.getName();
							if ("sid".equals(tagName2)) {
								String sid = elem.getTextTrim();
								resInfo.setSid(sid);
							} else if ("ste".equals(tagName2)) {
								String ste = elem.getTextTrim();
								resInfo.setSte(ste);
							} else if ("cid".equals(tagName2)) {
								String cid = elem.getTextTrim();
								resInfo.setCid(cid);
							} else if ("pid".equals(tagName2)) {
								String pid = elem.getTextTrim();
								resInfo.setPid(pid);
							} else if ("oid".equals(tagName2)) {
								String oid = elem.getTextTrim();
								resInfo.setOid(oid);
							} else if ("pn".equals(tagName2)) {
								String pn = elem.getTextTrim();
								resInfo.setPn(pn);
							} else if ("fm".equals(tagName2)) {
								String fm = elem.getTextTrim();
								resInfo.setFm(fm);
							} else if ("ft".equals(tagName2)) {
								String ft = elem.getTextTrim();
								resInfo.setFt(ft);
							} else if ("em".equals(tagName2)) {
								String em = elem.getTextTrim();
								resInfo.setEm(em);
							} else if ("sign".equals(tagName2)) {
								String sign = elem.getTextTrim();
								resInfo.setSign(sign);
							}
						}

					}
				}
			}

		} catch (DocumentException e) {
			log.error("解析7996交易返回xml出现异常", e);
		}
		return resInfo;
	}

	public static void main(String[] args) {
		String xml = "<?xml version=\"1.0\" encoding=\"GB2312\" ?>";
		xml += "<response>";
		xml += "<result>true</result>";
		xml += "<msg>下单成功,订单号:KZ1009161812281003</msg>";
		xml += "<data>";
		xml += "<sid>KZ1009161812281003</sid>";
		xml += "<ste>0</ste>";
		xml += "<cid>yibao1228</cid>";
		xml += "<pid>0103</pid>";
		xml += "<oid>TOPUP10000013610</oid>";
		xml += "<pn>13426200000</pn>";
		xml += "</data>";
		xml += "</response>";
		SevenNineNineSixRespInfo resp = parse(xml);
		System.out.println(resp.getSid());
		System.out.println(resp.getCid());
		System.out.println(resp.getOid());
	}
}
